﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;

namespace AnyOffice.general.system.address
{
    public partial class export : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int groupId = (Request.QueryString["group_id"] == null) ? 0 : int.Parse(Request.QueryString["group_id"].ToString());
            string str = "默认";
            if (groupId != 0)
            {
                DataTable table = new AddressGroupDao().SelectByGroupId(base.Conn, groupId, "");
                if (table.Rows.Count > 0)
                {
                    str = table.Rows[0]["GroupName"].ToString();
                }
            }
            string str2 = ".xls";
            while (HttpUtility.UrlEncode(str + str2, Encoding.UTF8).Length > 156)
            {
                str = str.Substring(0, str.Length - 1);
            }
            str = HttpUtility.UrlEncode(str + str2, Encoding.UTF8);
            AddressDao dao2 = new AddressDao();
            StringBuilder builder = new StringBuilder();
            builder.Append("<table border=1 bordercolor=#999999>\n");
            builder.Append("<tr>\n");
            builder.Append("  <td>姓名</td>\n");
            builder.Append("  <td>性别</td>\n");
            builder.Append("  <td>生日</td>\n");
            builder.Append("  <td>昵称</td>\n");
            builder.Append("  <td>职务</td>\n");
            builder.Append("  <td>配偶</td>\n");
            builder.Append("  <td>子女</td>\n");
            builder.Append("  <td>单位名称</td>\n");
            builder.Append("  <td>单位地址</td>\n");
            builder.Append("  <td>单位邮编</td>\n");
            builder.Append("  <td>单位电话</td>\n");
            builder.Append("  <td>单位传真</td>\n");
            builder.Append("  <td>家庭住址</td>\n");
            builder.Append("  <td>家庭邮编</td>\n");
            builder.Append("  <td>家庭电话</td>\n");
            builder.Append("  <td>手机</td>\n");
            builder.Append("  <td>小灵通</td>\n");
            builder.Append("  <td>电子邮件</td>\n");
            builder.Append("  <td>QQ号码</td>\n");
            builder.Append("  <td>MSN</td>\n");
            builder.Append("  <td>备注</td>\n");
            builder.Append("</tr>\n");
            DataTable table2 = dao2.SelectByGroupId(base.Conn, groupId, "");
            if (table2.Rows.Count > 0)
            {
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    builder.Append("<tr>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Name"] != DBNull.Value) ? table2.Rows[i]["Name"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + (Convert.ToBoolean(table2.Rows[i]["Sex"].ToString()) ? "男" : "女") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Birthday"] != DBNull.Value) ? table2.Rows[i]["Birthday"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["NickName"] != DBNull.Value) ? table2.Rows[i]["NickName"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Ministration"] != DBNull.Value) ? table2.Rows[i]["Ministration"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Mate"] != DBNull.Value) ? table2.Rows[i]["Mate"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Child"] != DBNull.Value) ? table2.Rows[i]["Child"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["DeptName"] != DBNull.Value) ? table2.Rows[i]["DeptName"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["DeptAddr"] != DBNull.Value) ? table2.Rows[i]["DeptAddr"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["DeptPost"] != DBNull.Value) ? table2.Rows[i]["DeptPost"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["DeptTel"] != DBNull.Value) ? table2.Rows[i]["DeptTel"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["DeptFax"] != DBNull.Value) ? table2.Rows[i]["DeptFax"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["HomeAddr"] != DBNull.Value) ? table2.Rows[i]["HomeAddr"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["HomePost"] != DBNull.Value) ? table2.Rows[i]["HomePost"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["HomeTel"] != DBNull.Value) ? table2.Rows[i]["HomeTel"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Mobile"] != DBNull.Value) ? table2.Rows[i]["Mobile"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Bp"] != DBNull.Value) ? table2.Rows[i]["Bp"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Email"] != DBNull.Value) ? table2.Rows[i]["Email"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Oicq"] != DBNull.Value) ? table2.Rows[i]["Oicq"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Msn"] != DBNull.Value) ? table2.Rows[i]["Msn"].ToString() : "") + "</td>\n");
                    builder.Append("  <td>" + ((table2.Rows[i]["Note"] != DBNull.Value) ? table2.Rows[i]["Note"].ToString() : "") + "</td>\n");
                    builder.Append("</tr>\n");
                }
            }
            builder.Append("</table>\n");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + str);
            Response.Clear();
            Response.Buffer = true;
            Response.BinaryWrite(Encoding.Default.GetBytes(builder.ToString()));
            Response.End();
        }

    }
}
